//
//  ChatListViewController.m
//  rongyun
//
//  Created by lanou3g on 16/1/8.
//  Copyright © 2016年 revocher. All rights reserved.
//

#import "ChatListViewController.h"
#import "ChatViewController.h"
#import <RongIMKit/RongIMKit.h>
#import <AVOSCloud.h>
#import "RootViewController.h"
@interface ChatListViewController ()<UITextFieldDelegate>
@property(strong,nonatomic) NSString *nameStr;

@end

@implementation ChatListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //设置需要显示哪些类型的会话
    [self setDisplayConversationTypes:@[@(ConversationType_PRIVATE),
                                        @(ConversationType_DISCUSSION),
                                        @(ConversationType_CHATROOM),
                                        @(ConversationType_GROUP),
                                        @(ConversationType_APPSERVICE),
                                        @(ConversationType_SYSTEM)]];
    //设置需要将哪些类型的会话在会话列表中聚合显示
    [self setCollectionConversationType:@[@(ConversationType_DISCUSSION),
                                          @(ConversationType_GROUP)]];
    
    UIBarButtonItem *leftIrem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction:)];
    self.navigationItem.leftBarButtonItem = leftIrem;
    
    UIBarButtonItem *rightitem = [[UIBarButtonItem alloc]initWithTitle:@"添加聊天对象" style:UIBarButtonItemStylePlain  target:self  action:@selector(rightItemAction:)];
    self.navigationItem.rightBarButtonItem = rightitem;
   

    
    
    
}
//- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType
//         conversationModel:(RCConversationModel *)model
//               atIndexPath:(NSIndexPath *)indexPath {
//    RCConversationViewController *conversationVC = [[RCConversationViewController alloc]init];
//    conversationVC.conversationType = model.conversationType;
//    conversationVC.targetId = model.targetId;
//    conversationVC.tabBarController.hidesBottomBarWhenPushed = YES;
//    
//    conversationVC.title = @"想显示的会话标题";
//    [self.navigationController presentViewController:conversationVC animated:YES completion:nil];
//    
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)leftItemAction:(UIBarButtonItem *)sender{
    RootViewController *affVC = [[RootViewController alloc]init];
    affVC.selectedIndex = 2;
    [self.navigationController presentViewController:affVC animated: YES completion:nil];
    
}
-(void)rightItemAction:(UIBarButtonItem*)sender{
    ChatViewController *chat = [[ChatViewController alloc]init];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提醒" message:@"请输入想聊天的对象" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"请输入对方的昵称";
        //设置会话的目标会话ID。（单聊、客服、公众服务会话为对方的ID，讨论组、群聊、聊天室为会话的ID）
        textField.delegate = self;
        
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        //新建一个聊天会话View Controller对象
        //设置会话的类型，如单聊、讨论组、群聊、聊天室、客服、公众服务会话等
        chat.targetId =  self.nameStr;
   
        chat.conversationType = ConversationType_PRIVATE;
        
        //设置聊天会话界面要显示的标题
        chat.title = self.nameStr;
        //显示聊天会话界面
        NSLog(@"%@+++++++++",chat.targetId);
        UINavigationController * chatNC = [[UINavigationController alloc]initWithRootViewController:chat];
        [self.navigationController presentViewController:chatNC animated:YES completion:nil];
        chat.key = 2;
        chat.name = self.nameStr;
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
    
    
    


    
}



- (void)setConversationAvatarStyle:(RCUserAvatarStyle)avatarStyle{
    avatarStyle = RC_USER_AVATAR_CYCLE;


}


-(void)textFieldDidEndEditing:(UITextField *)textField{
    self.nameStr = textField.text;
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
